/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @package    Feather-CM
 * @subpackage Javascript
 * @copyright  Copyright (c) 2006, Julien PORTALIER
 * @link       http://portalier.com/feather
 * @license    http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

var Filer = {};
Filer.Base = new Class(
{
	initialize: function(filer)
	{
		this.filer  = $(filer);
		this.files  = $E('.files', this.filer);
		this.upload = $('fileUpload').hide();
		this.mkdir  = $('fileMkdir').hide();
		
		new Fx.Accordion($ES('dl.files dt.file', this.filer), $ES('dl.files dd.file', this.filer));
		new Element('a', {'href': '#'}, _('+ Send another file')).addEvent('click', this.addFile.bind(this)).injectBefore($E('div.submit', this.upload));
		
		$ES('.filertoolbar a').each(function(bt) {
			bt.addEvent('click', this.onToolbarButton.bind(this));
		}, this);
	},
	
	onToolbarButton: function(evt)
	{
		evt = new Event(evt).stop();
		var target = evt.target();
		while(target.getTag() != 'a')
			target = target.parentNode;
		
		var form = $(target.getAttribute('class'));
		var display = form.visible();
		
		$ES('.filertoolbar a').each(function(bt) { $(bt.getAttribute('class')).hide(); });
		
		if (!display)
			form.show();
	},
	
	addFile: function(evt)
	{
		new Event(evt).stop();
		new Element('input', {
			'type':  'file',
			'class': 'file',
			'name':  $E('input[type=file]', this.upload).getProperty('name')
		}).injectInside($E('div.required', this.upload));
	}
});